<?php
//个人中心页面  需要验证登录
if(!$_SESSION['uid']){
    $_ajax=NoBadStr($_REQUEST['_ajax']);
    if($_ajax){
        $result=[
            'code'=>'10',
            'msg'=>'请登录后在操作!'
        ];
        AjaxReturn($result);
    }else{
        header("Location:".MU('login/index'));
    }
}
$beetArr=[
    '1'=>'定位胆',
    '7'=>'五星直选',
    '8'=>'4星直选',
    '11'=>'后三',
    '9'=>'前三',
    '12'=>'前二',
    '4'=>'不定位',
    '2'=>'大小单双',
    '13'=>'任选二',
    '14'=>'任选三',
    '15'=>'任选四',
];
if($a=='order'){
    //下彩票订单
    $data=[
        'c_id'=>NoBadStr($_REQUEST['c_id']),
        'uid'=>$_uid,
        'dateline'=>TIMESTAMP,
        'beet'=>NoBadStr($_REQUEST['beet']),
        'betCode'=>NoBadStr($_REQUEST['betCode']),
        'spid'=>NoBadStr($_REQUEST['spid']),
        'selC'=>NoBadStr($_REQUEST['selC']),
    ];
    $data['betCode']=str_replace('_','@',$data['betCode']);
    
    if(!$data['c_id']){
        $result=[
            'code'=>'3',
            'msg'=>'参数丢失，请刷新重新购买!'
        ];
        AjaxReturn($result);
    }
    //获取当前开奖期数
    $period=$db->getrow('select * from '.Table('lottery_period').' where cid='.$data['c_id'].' and start<='.TIMESTAMP.' and stop>'.TIMESTAMP);
    if(!$period['id']){
        $result=[
            'code'=>'4',
            'msg'=>'没有可购买彩票，请刷新重试!'
        ];
        AjaxReturn($result);
    }
    $data['i_id']=$period['id'];
    $data['i_num']=$period['period'];

    //计算费用
    if(in_array($data['c_id'],['1','2','9','11'])){
        //三分时时彩  重庆时时彩 game.4
        require_once LIB_PATH . 'lottery.1.php';
    }elseif (in_array($data['c_id'],['3','6','8','10','12','13'])){
        //北京PK10  3分PK10  game.7
        require_once LIB_PATH . 'lottery.3.php';
    }elseif (in_array($data['c_id'],['4'])){
        //幸运28 game.41
        require_once LIB_PATH . 'lottery.4.php';
    }elseif (in_array($data['c_id'],['14'])){
        //安徽快三  game.6
        require_once LIB_PATH . 'lottery.14.php';
    }elseif (in_array($data['c_id'],['15','16'])){
        //福彩3D games.3s
        require_once LIB_PATH . 'lottery.15.php';
    }
    $price=doBeetPrice($data['betCode'],$data['spid'],$data['selC']);
    if($price<=0){
        $result=[
            'code'=>'2',
            'msg'=>'彩票金额不能为0!'
        ];
        AjaxReturn($result);
    }
    $user=$db->getrow('select * from '.Table('user').' where id='.$_uid);
    if($user['money']<$price){
        $result=[
            'code'=>'1',
            'msg'=>'账户余额不足，请充值!'
        ];
        AjaxReturn($result);
    }
    $data['money']=$price;

    //扣除金额
    $db->exec('update '.Table('user').' set money=money-'.$price.' where id='.$_uid);
    $id=$db->add('lottery_order',$data);
    $log=[
        'uid'=>$_uid,
        'fee'=>-$price,
        'money'=>($user['money']-$price),
        'dateline'=>TIMESTAMP,
        'notes'=>'购买彩票,订单ID:'.$id,
        'tid'=>'2',
    ];
    $db->add('user_money_log',$log);
    $result=[
        'code'=>'0',
        'msg'=>'下单成功'
    ];
    AjaxReturn($result);
}elseif($a=='index'){
    //个人中心
    $todayFee=$db->getrow('select sum(fee) as total from '.Table('user_money_log').' where uid='.$_uid.' and tid in (2,3)');

    $tpl->display('mine/index');
}elseif ($a=='set'){

    $tpl->display('mine/set');
}elseif ($a == 'setpaypwd'){
    //修改支付密码
    $tpl->display('mine/setpaypwd');
}elseif($a == 'ajaxpaypwd'){
    //提交修改支付密码
    $pwd=NoBadStr($_REQUEST['pwd']);
    $newpwd=NoBadStr($_REQUEST['newpwd']);
    if($_Y['user']['paypassword']){
        if($_Y['user']['paypassword']!=Password($pwd)){
            $result=[
                'code'=>'1',
                'msg'=>'支付密码错误'
            ];
            AjaxReturn($result);
        }
    }
    if(!$newpwd){
        $result=[
            'code'=>'2',
            'msg'=>'支付密码不能为空'
        ];
        AjaxReturn($result);
    }
    $db->exec('update '.Table('user').' set paypassword=\''.Password($newpwd).'\' where id='.$_Y['user']['id']);
    $result=[
        'code'=>'0',
        'msg'=>'支付密码修改成功'
    ];
    AjaxReturn($result);
}elseif ($a == 'setpwd'){
    //修改登录密码
    $tpl->display('mine/setpwd');
}elseif($a == 'ajaxpwd'){
    $pwd=NoBadStr($_REQUEST['pwd']);
    $newpwd=NoBadStr($_REQUEST['newpwd']);
    if($_Y['user']['passwd']!=Password($pwd)){
        $result=[
            'code'=>'1',
            'msg'=>'登录密码错误'
        ];
        AjaxReturn($result);
    }
    if(!$newpwd){
        $result=[
            'code'=>'2',
            'msg'=>'登录密码不能为空'
        ];
        AjaxReturn($result);
    }
    $db->exec('update '.Table('user').' set passwd=\''.Password($newpwd).'\' where id='.$_Y['user']['id']);
    $result=[
        'code'=>'0',
        'msg'=>'登录密码修改成功'
    ];
    AjaxReturn($result);
}
elseif($a == 'deposit'){
    //充值
    $tpl->display('mine/deposit');
}elseif($a == 'withdraw'){
    //提现绑卡
    $bank=$db->getrow('select * from '.Table('user_bank').' where uid='.$_uid);
    if($bank['id']){
        //已经绑卡
        $tpl->display('mine/withdrawstep2');
    }else{
        //去绑卡
        $tpl->display('mine/withdraw');
    }
}elseif($a == 'ajaxwithdraw'){
    //绑定银行卡
    $bank=NoBadStr($_REQUEST['bank']);
    $bankNum=NoBadStr($_REQUEST['bankNum']);
    $name=NoBadStr($_REQUEST['name']);
    $province=NoBadStr($_REQUEST['province']);
    $city=NoBadStr($_REQUEST['city']);
    $paypwd=NoBadStr($_REQUEST['paypwd']);

    if($_Y['user']['paypassword']!=Password($paypwd)){
        $result=[
            'code'=>'1',
            'msg'=>'交易密码错误'
        ];
        AjaxReturn($result);
    }
    if(!$bank||!$bankNum||!$name||!$province||!$city){
        $result=[
            'code'=>'2',
            'msg'=>'请填写个人银行卡信息'
        ];
        AjaxReturn($result);
    }
    $ck=$db->getrow('select * from '.Table('user_bank').' where uid='.$_uid);
    $data=[
        'bank'=>$bank,
        'bankNum'=>$bankNum,
        'name'=>$name,
        'province'=>$province,
        'city'=>$city,
    ];
    if($ck['id']){
        $db->update('user_bank',$data,'id='.$ck['id']);
    }else{
        $data['uid']=$_uid;
        $db->add('user_bank',$data);
    }
    $result=[
        'code'=>'0',
        'msg'=>'绑定成功'
    ];
    AjaxReturn($result);
}elseif($a == 'ajaxwithdrawstep2'){
    $money=NoBadStr($_REQUEST['money']);
    $money=intval($money);
    $paypwd=NoBadStr($_REQUEST['paypwd']);
    if($_Y['user']['paypassword']!=Password($paypwd)){
        $result=[
            'code'=>'1',
            'msg'=>'交易密码错误'
        ];
        AjaxReturn($result);
    }
    if($money<10){
        $result=[
            'code'=>'3',
            'msg'=>'提现金额不能小于10'
        ];
        AjaxReturn($result);
    }
    if($_Y['user']['money']<$money){
        $result=[
            'code'=>'2',
            'msg'=>'账户余额不足'
        ];
        AjaxReturn($result);
    }
    $db->exec('update '.Table('user').' set money=money-'.$money.' where id='.$_uid);
    $log=[
        'uid'=>$_uid,
        'fee'=>-$money,
        'money'=>($user['money']-$money),
        'dateline'=>TIMESTAMP,
        'notes'=>'用户提现',
        'tid'=>'3',
    ];
    $lid=$db->add('user_money_log',$log);

    //录入提现表
    $data=[
        'money'=>$money,
        'dateline'=>TIMESTAMP,
        'status'=>0,
        'uid'=>$_uid,
        'sn'=>date("YmdHis").'_'.$lid,
    ];
    $db->add('user_money_cash',$data);

    $result=[
        'code'=>'0',
        'msg'=>'提交成功，我们会在7个工作日内给你转账，请耐心等待'
    ];
    AjaxReturn($result);
}elseif($a == 'cashlog'){
    //提现记录
    $list=$db->getall('select * from '.Table('user_money_cash').' where uid='.$_uid.' order by id desc');
    $tpl->display('mine/cashlog');
}elseif($a == 'cashdetail'){
    $id=NoBadStr($_REQUEST['id']);
    if(!$id){
        ShowMsg('参数丢失');
    }
    $data=$db->getrow('select * from '.Table('user_money_cash').' where id='.$id);
    $bank=$db->getrow('select * from '.Table('user_bank').' where uid='.$_uid);

    $tpl->display('mine/cashdetail');
}elseif($a == 'betlist'){
    $onlywin=NoBadStr($_REQUEST['onlywin']);
    //投注记录
    if($_REQUEST['tag']=='ajax'){
        $page=NoBadStr($_REQUEST['page']);
        $page=intval($page) ? intval($page) : 1;
        $perPage=intval(NoBadStr($_REQUEST['perpage']));
        $perPage=$perPage ? $perPage : 20;

        $whereSql='';
        if($onlywin){
            $whereSql.=' and isdraw=1';
        }
        $list=$db->getall('select * from '.Table('lottery_order').' where uid='.$_uid.$whereSql.' order by id desc limit '.($page-1)*$perPage.','.$perPage);
        foreach ($list as $key => $val){
            $val['url']=MU('mine/betdetail',['id'=>$val['id']]);
            $cate=$db->getrow('select * from '.Table('lottery').' where id='.$val['c_id']);
            $val['cname']=$cate['title'];
            $val['datetime']=date("Y-m-d H:i:s");
            $list[$key]=$val;
        }
        $result=[
            'code'=>'0',
            'list'=>$list,
            'total'=>count($list)
        ];
        AjaxReturn($result);
    }
    $tpl->display('mine/betlist');
}elseif($a == 'betdetail'){
    $id=NoBadStr($_REQUEST['id']);
    if(!$id){
        ShowMsg('参数丢失');
    }
    $order=$db->getrow('select * from '.Table('lottery_order').' where id='.$id);
    $lottery=$db->getrow('select * from '.Table('lottery').' where id='.$order['c_id']);
    $period=$db->getrow('select * from '.Table('lottery_period').' where id='.$order['i_id']);
    $tpl->display('mine/betdetail');
}elseif($a == 'account'){
    //账户明细
    $only=NoBadStr($_REQUEST['only']);
    //账户明细
    if($_REQUEST['tag']=='ajax'){
        $page=NoBadStr($_REQUEST['page']);
        $page=intval($page) ? intval($page) : 1;
        $perPage=intval(NoBadStr($_REQUEST['perpage']));
        $perPage=$perPage ? $perPage : 20;

        $whereSql='';
        if($only){
            $whereSql.=' and tid=1';//充值
        }
        $list=$db->getall('select * from '.Table('user_money_log').' where uid='.$_uid.$whereSql.' order by id desc limit '.($page-1)*$perPage.','.$perPage);
        foreach ($list as $key => $val){
            $val['datetime']=date("Y-m-d H:i:s");
            if($val['tid']=='1'){
                $val['cname']='充值';
            }elseif ($val['tid']=='2'){
                $val['cname']='购买彩票';
            }elseif ($val['tid']=='3'){
                $val['cname']='提现';
            }elseif ($val['tid']=='4'){
                $val['cname']='中奖';
            }
            $list[$key]=$val;
        }
        $result=[
            'code'=>'0',
            'list'=>$list,
            'total'=>count($list)
        ];
        AjaxReturn($result);
    }
    $tpl->display('mine/account');
}
?>